package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.common.PaginationVO;
import com.itheima.exception.BusinessException;
import com.itheima.mapper.AdvposMapper;
import com.itheima.service.AdvposService;
import com.itheima.vo.advVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.util.List;


@Slf4j
@Service
@RequiredArgsConstructor
public class AdvposServiceImpl implements AdvposService {
    private final AdvposMapper advposMapper;

    /**
     * 页
     *
     * @param page       页
     * @param limit      限度
     * @param advposdesc advposdesc
     * @param advposname advposname
     * @return {@link PaginationVO}<{@link advVO}>
     */
    @Override
    public PaginationVO<advVO> page(String page, String limit, String advposdesc, String advposname) {
        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(limit));
        List<advVO> cate = advposMapper.getPageContent(advposdesc,advposname);
        cate.forEach(s->{
            Integer count = advposMapper.getCount(s.getId());
            s.setAdvposnum(count);
        });
        PageInfo<advVO> pageInfo = new PageInfo<>(cate);
        long count = pageInfo.getTotal();
        int totalPages = pageInfo.getPages();
        int pageSize = pageInfo.getPageSize();
        int currentPage = pageInfo.getNextPage();
        List<advVO> advVOList = pageInfo.getList();

        PaginationVO<advVO> paginationVO = new PaginationVO<>();
        paginationVO.setCount(count);
        paginationVO.setTotalPages(totalPages);
        paginationVO.setPageSize(pageSize);
        paginationVO.setCurrentPage(currentPage);
        paginationVO.setData(advVOList);
        return paginationVO;
    }

    /**
     * 获取所有列表
     *
     * @return {@link List}<{@link advVO}>
     */
    @Override
    public List<advVO> getAllList() {
        List<advVO> cate = advposMapper.getAllList();
        return cate;
    }

    /**
     * 添加advpos
     *
     * @param adv 副词。
     */
    @Override
    public void addAdvpos(advVO adv) {
       Integer count = advposMapper.getCountByName(adv.getAdvposname());
       if (count>0){
           throw new BusinessException("名字已经存在");
       }
        adv.setDefault_data("1");
        advposMapper.addAdvpos(adv);
    }

    /**
     * 行信息advpos
     *
     * @param id id
     * @return {@link advVO}
     */
    @Override
    public advVO rowInfoAdvpos(Integer id) {
        advVO adv = advposMapper.rowInfoAdvpos(id);
        return adv;
    }

    /**
     * 更新advpos
     *
     * @param adv 副词。
     */
    @Override
    public void updateAdvpos(advVO adv) {
        Integer count = advposMapper.getCountByNameAndId(adv.getAdvposname(),adv.getId());
        if (count>0){
            throw new BusinessException("名字已经存在");
        }
        advposMapper.updateAdvpos(adv);
    }

    /**
     * 删除advpos
     *
     * @param id id
     */
    @Override
    public void deleteAdvpos(Integer id) {
        advposMapper.deleteAdvpos(id);
    }
}
